package com.zhuiYun.project.api.handworkDiy.service.serviceImpl;

import com.zhuiYun.project.api.commodity.entity.Commodity;
import com.zhuiYun.project.api.handworkDiy.entity.HandworkDiy;
import com.zhuiYun.project.api.handworkDiy.mapper.handworkMapper;
import com.zhuiYun.project.api.handworkDiy.model.HandworkDiyModel;
import com.zhuiYun.project.api.handworkDiy.service.handworkService;
import com.zhuiYun.project.api.order.model.CommorderModel;
import com.zhuiYun.project.api.slideshow.model.slidesShowModel;
import com.zhuiYun.project.common.response.CommonResult;
import com.zhuiYun.project.util.BeanUtils;
import com.zhuiYun.project.util.ObjectUtils;
import com.zhuiYun.project.util.UploadFileUtils;
import com.zhuiYun.project.util.pageUtil.MybatisPageHelper;
import com.zhuiYun.project.util.pageUtil.PageRequest;
import com.zhuiYun.project.util.pageUtil.PageResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class handworkSericeImpl  implements handworkService {

    @Autowired
    handworkMapper handworkmapper;


    /**
     * @Author 小江
     * @Description //TODO diy全查 tk单表查询
     * @Date 14:49 2023/4/6
     **/
    @Override
    public PageResult diyList(PageRequest pageResult, String diyName) {
        Example example = new Example(HandworkDiy.class);
        MybatisPageHelper.startPage(pageResult);
        example.createCriteria().andEqualTo("delFlag",0);
        List<HandworkDiy> handworkDiys = handworkmapper.selectByExample(example);
        return   MybatisPageHelper.getPageResult(handworkDiys);
    }


    /**
     * @Author 小江
     * @Description //TODO diy 两表查询
     * @Date 14:42 2023/4/7
     **/
    @Override
    public PageResult diysList(PageRequest pageResult, String diyName) {
        if(StringUtils.isEmpty(diyName)){
            diyName="";
        }
        int pageNum = pageResult.getPageNum();
        List<HandworkDiyModel> handworkDiyModels = handworkmapper.diysList(diyName,pageResult);
        MybatisPageHelper.startPage(pageResult);
        return MybatisPageHelper.getPageResult(handworkDiyModels);
    }


    /**
     * @Author 小江
     * @Description //TODO 查询删除的diy订单
     * @Date 9:40 2023/4/18
     **/
    @Override
    public PageResult regretDiy(PageRequest pageResult, String diyName) {
        if(StringUtils.isEmpty(diyName)){
            diyName="";
        }
        int pageNum = pageResult.getPageNum();
        List<HandworkDiyModel> handworkDiyModels = handworkmapper.regretDiy(diyName,pageResult);
        MybatisPageHelper.startPage(pageResult);
        return MybatisPageHelper.getPageResult(handworkDiyModels);
    }


    /**
     * @Author 小江
     * @Description //TODO 查询回收站的diy信息
     * @Date 11:31 2023/4/18
     **/
    @Override
    public PageResult recycleDiy(PageRequest pageResult, String diyName) {
        if(StringUtils.isEmpty(diyName)){
            diyName="";
        }
        int pageNum = pageResult.getPageNum();
        List<HandworkDiyModel> handworkDiyModels = handworkmapper.recycleDiy(diyName,pageResult);
        MybatisPageHelper.startPage(pageResult);
        return MybatisPageHelper.getPageResult(handworkDiyModels);
    }

    /**
     * @Author 小江
     * @Description //TODO 删除diy信息进入回收站
     * @Date 11:31 2023/4/18
     **/
    @Override
    public int recycleDel(Integer id) {
        HandworkDiy handworkdiy = new HandworkDiy();
        handworkdiy.setId(id);
        handworkdiy.setDelFlag(2);
        int i = handworkmapper.updateByPrimaryKeySelective(handworkdiy);
        return i;
    }

    @Override
    public int partDiy(Integer id) {
        int i = handworkmapper.deleteByPrimaryKey(id);
        return i;
    }


    /**
     * @Author 小江
     * @Description //TODO 回复下架的商品
     * @Date 9:54 2023/4/18
     **/
    @Override
    public int redemptionDiy(Integer id) {
        HandworkDiy handworkdiy = new HandworkDiy();
        handworkdiy.setId(id);
        handworkdiy.setDelFlag(0);
        int i = handworkmapper.updateByPrimaryKeySelective(handworkdiy);
        return i;
    }




    /**
     * @Author 小江
     * @Description //TODO diy删除
     * @Date 9:51 2023/4/7
     **/
    @Override
    public int handworkDel(Integer id) {
        HandworkDiy handworkdiy = new HandworkDiy();
        handworkdiy.setId(id);
        handworkdiy.setDelFlag(1);
        int i = handworkmapper.updateByPrimaryKeySelective(handworkdiy);
        return i;
    }
    /**
     * @Author 小江
     * @Description //TODO 两表查所有（diy和商品表）
     * @Date 8:49 2023/4/11
     **/

    @Override
    public CommonResult commanddiylist(HandworkDiyModel handworkDiyModel) {
        List<HandworkDiyModel> handworkDiyModels = handworkmapper.commanddiylist((handworkDiyModel));
        return CommonResult.ok(handworkDiyModels);
    }


    /**
     * @Author 小江
     * @Description //TODO diy查询信息
     * @Date 8:49 2023/4/11
     **/
    @Override

    public CommonResult ditylcomlist(HandworkDiyModel handworkDiyModel) {
        Example example = new Example(HandworkDiy.class);
        List<HandworkDiy> HandworkDiy = handworkmapper.selectByExample(example);
        return CommonResult.ok(HandworkDiy);
    }

    /**
     * @Author 小江
     * @Description //TODO diy修改信息
     * @Date 18:46 2023/4/11
     **/
    @Override
    public int CommodityUpdate(MultipartFile diyImageId, MultipartFile[] diyDetailsImageId, HandworkDiyModel handworkDiyModel) {
        handworkDiyModel.setUpdateTime(new Date());

        int i=0;
        if(diyImageId!=null){
            String upl = UploadFileUtils.uploadFile(diyImageId);
            String s = UploadFileUtils.uploadFile(diyImageId);
            if(!StringUtils.isEmpty(s)){
                handworkDiyModel.setDiyImageId(upl);
            }
        }
        if (diyDetailsImageId !=null){
            List<String> diyDetailsImageIds= new ArrayList<>();
            for (int a=0;a < diyDetailsImageId.length;a++){
                String url  = UploadFileUtils.uploadFile(diyDetailsImageId[a]);
                if (ObjectUtils.isNotEmpty(url));{
                    diyDetailsImageIds.add(url);
                }
            }
            handworkDiyModel.addDiyDetailsImageId(StringUtils.join(diyDetailsImageIds,','));
        }
        HandworkDiy handworkDiy = new HandworkDiy();
        BeanUtils.copy(handworkDiyModel, handworkDiy);
        i = handworkmapper.updateByPrimaryKeySelective(handworkDiy);
        return i;
    }



    /**
     * @Author 小江
     * @Description //TODO diy添加
     * @Date 9:51 2023/4/7
     **/
    @Override
    public int diyAdd(MultipartFile diyImageId, MultipartFile[] diyDetailsImageId, HandworkDiyModel handworkDiyModel) {
        handworkDiyModel.setCreateTime(new Date());
        handworkDiyModel.setDelFlag(1);
        int i=0;
        if(diyImageId!=null){
            String upl = UploadFileUtils.uploadFile(diyImageId);
            String s = UploadFileUtils.uploadFile(diyImageId);
            if(!StringUtils.isEmpty(s)){
                handworkDiyModel.setDiyImageId(upl);
            }
        }
            if (diyDetailsImageId !=null){
                List<String> diyDetailsImageIds= new ArrayList<>();
                for (int a=0;a < diyDetailsImageId.length;a++){
                    String url  = UploadFileUtils.uploadFile(diyDetailsImageId[a]);
                    if (ObjectUtils.isNotEmpty(url));{
                        diyDetailsImageIds.add(url);
                    }
                }
                handworkDiyModel.setDiyDetailsImageId(StringUtils.join(diyDetailsImageIds,","));
            }
        HandworkDiy handworkDiy = new HandworkDiy();
        BeanUtils.copy(handworkDiyModel, handworkDiy);

            i = handworkmapper.insertSelective(handworkDiy);
        return i;
    }





}
